package Wallpaper::Configuration;
$VERSION = sprintf "%.3f", 1 + substr( q$Revision: 159 $, 9 ) / 1000;

require Exporter;
use Storable qw( dclone );
use Hash::Merge qw( merge );
use Config::General;

@ISA       = qw( Exporter );
@EXPORT_OK = qw( %datastore );

our %datastore;

use strict;

=item * setdefaults

setdefaults sets all the configuration defaults, and if a user config file is
present, it will read it and overwrite the defaults.

See the configuration for a description of configuration options.

This function has no parameters and returns nothing.  However, by the finish
of this function - a full populated %config hash should be present.

=cut

### constructor
sub new {
    my $class = shift;

    my ( %system_config, %user_config, %self );

    %system_config = (
        "general" => {
                debug => 0,
                bgdir => "$ENV{'HOME'}/.backgrounds",
            exe_scale => "feh --bg-scale",
            clean_shm => 1,
        schema_preset => [ qw( "555555" "666666" "777777") ],
             photoset => [ qw( "Early" "Early" "Early" "Early" "Early" "Morning" 
                               "Food/Breakfast" "Morning" "Morning" "Morning" "Work/Arriving" 
                               "Work/Arriving" "Food/Lunch" "Work/Hacking" "Work/Hacking" "Afternoon" "Afternoon"
                               "Afternoon" "Afternoon" "Food/Dinner" "Evening" "Evening" "Evening" ) ]

        },
        "display" => { 
           "TEMPLATE" => { 
                   enable => 0,
               resolution => "1024x768",
                  "conky" => {
                       enable => 0,
                     template => "$ENV{'HOME'}/.conkyrc.template",
                       output => "$ENV{'HOME'}/.conkyrc"
                             },
                    "gtk" => { 
                       enable => 0,
                     template => "$ENV{'HOME'}/.gtkrc.template",
                       output => "$ENV{'HOME'}/.gtkrc"
                             },
                     "ob" => { 
                       enable => 0,
                     template => "$ENV{'HOME'}/.themes/custom/openbox-3/themerc.template",
                       output => "$ENV{'HOME'}/.themes/custom/openbox-3/themerc"
                             },
                  "urxvt" => { 
                       enable => 0,
                     theme_op => "10",
                     black_op => "70",
                         blur => "5",
                          dir => "/dev/shm/walls"
                             },
                 "mangle" => { 
                       enable => 0,
                   bordersize => 2,
                  tbar_height => 26,
                  rbar_width  => 342
                             },
                "collage" => { 
                       enable => 0,
                    container => "!315x250",
                        photo => "!265x200",
                       "text" => {
                             font => "/usr/share/fonts/TTF/aquafont.ttf",
                             size => 15,
                          content => " ... the next background will be ... ",
                                x => 30,
                                y => 270
                                 },
                    "border1" => { 
                              geo => "5x5",
                            color => "white"
                                 },
                    "border2" => { 
                              geo => "1x1",
                            color => "grey40"
                                 }
                             }
                         }
                     }
    );

    my @items = qw( 0 1 2 3 4 );

    for my $primary ( @items ) {
        for my $secondary ( @items ) {
            my $display = $primary . "." . $secondary;
            $system_config{ 'display' }{ $display } = dclone( $system_config{ 'display' }{ 'TEMPLATE' } );
        }
    }
    delete $system_config{ 'display' }{ 'TEMPLATE' };

    my $user_config = Config::General->new( -ConfigFile => $ENV{ 'HOME' } . "/.wallrc" );
    my %user_config = $user_config->getall;

    Hash::Merge::specify_behavior(
        {  SCALAR => { SCALAR => sub { $_[1] },
                        ARRAY => sub { [ $_[0], @{ $_[1] } ] },
                         HASH => sub { $_[1] }
                     },
            ARRAY => { SCALAR => sub { $_[1]; },
                        ARRAY => sub { [ @{ $_[1] } ]; },
                         HASH => sub { $_[1]; }
                     },
             HASH => { SCALAR => sub { $_[1]; },
                        ARRAY => sub { [ values %{ $_[0] }, @{ $_[1] } ]; },
                         HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ); }
                     }
        },
        "My Behavior"
                                 );

    %self = %{ merge( \%system_config, \%user_config ) };

    bless( \%self, $class );
  return \%self;
} ## end sub new

### accessors
sub general {
    my ( $class ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'general' };
}

sub schema {
    my ( $class ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'general' }{ 'schema_preset' };
}

sub photoset {
    my ( $class ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'general' }{ 'photoset' };
}

sub all_display {
    my ( $class ) = @_;
    my $configdata = $class->new;
  return $configdata->{ 'display' };
}

sub display {
    my ( $class, $number ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'display' }{ $number };
}

sub conky {
    my ( $class, $number ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'display' }{ $number }{ 'conky' };
}

sub urxvt {
    my ( $class, $number ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'display' }{ $number }{ 'urxvt' };
}

sub mangle {
    my ( $class, $number ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'display' }{ $number }{ 'mangle' };
}

sub collage {
    my ( $class, $number ) = @_;
    my $configdata = $class->new;

  return $configdata->{ 'display' }{ $number }{ 'collage' };
}

sub dumpconfig {
    my ( $class, $number ) = @_;
    my $configdata = $class->new;

    print "Dumping Config\n";

    # Now print the option back out if there's a debug condition.
    #if ( $config{ 'debug' } ) {
    #    print "   Configuration options are:\n";
    #    foreach my $opt ( keys %config ) {
    #        if ( $opt =~ /^schema_preset|^photoset/ ) {
    #            print "\t$opt @ [ ";
    #            for my $list ( @{ $config{ $opt } } ) {
    #                print "$list ";
    #            }
    #            print "]\n";
    #        } else {
    #            print "\t$opt = $config{$opt}\n";
    #        }
    #    } ## end foreach my $opt ( keys %config)
    #} ## end if ( $config{ 'debug' ...
} ## end sub dumpconfig

1;
